/*/////////////////////////////////////////////////////////////////////////////////
/// An
///    ___   ____ ___ _____ ___  ____
///   / _ \ / ___|_ _|_   _/ _ \|  _ \
///  | | | | |  _ | |  | || | | | |_) |
///  | |_| | |_| || |  | || |_| |  _ <
///   \___/ \____|___| |_| \___/|_| \_\
///                              File
///
/// Copyright (c) 2008-2009 Ismail TARIM <ismail@royalspor.com> and the Ogitor Team
//
//This program is free software; you can redistribute it and/or modify it under
//the terms of the GNU Lesser General Public License as published by the Free Software
//Foundation; either version 2 of the License, or (at your option) any later
//version.
//
//This program is distributed in the hope that it will be useful, but WITHOUT
//ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
//FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public License along with
//this program; if not, write to the Free Software Foundation, Inc., 59 Temple
//Place - Suite 330, Boston, MA 02111-1307, USA, or go to
//http://www.gnu.org/copyleft/lesser.txt.
////////////////////////////////////////////////////////////////////////////////*/

#pragma once

namespace Ogre
{
    class Terrain;
}

namespace Ogitors
{

struct TERRAINLAYERDATA
{
    float size;
    Ogre::StringVector textures;
};
    
class OgitorExport CPagedTerrainEditor : public CBaseEditor
{
    friend class CPagedTerrainEditorFactory;
    friend class CPagedTerrainManager;
public:
    static OgitorsPropertyDefMap propertyDefs;

    /// Returns the Factory associated with this object
	static CBaseEditorFactory   *getFactory();
    /// Returns the Factory associated with this object can be overridden
	virtual CBaseEditorFactory  *getFactoryDynamic();

    void             setPosition(const Ogre::Vector3& position, bool display);
    Ogre::Vector3    getPosition();
    TERRAINLAYERDATA *getLayerData(unsigned int index);

    /// Called by Serializer to write custom files during an export
	virtual void     onSave();
    /// Override to change behavior on Name Changes
    virtual void     setNameImpl(Ogre::String name, bool display);
    virtual void     processParameters(Ogre::NameValuePairList &params);
    virtual bool     load();
    virtual bool     unLoad();
	virtual void     getObjectProperties(Ogre::NameValuePairList &retList);
	virtual bool	 setupPropertiesWnd(bool present);
  	virtual bool     handleCustomFunction(int BIND_ID, Ogre::String val);
    void             changeMapSize(int newsize);
    const Ogre::String& getMaterialName();

	/// This function is called when user right clicks a property to get that property specific popup menu
	/// returns false if no menu present (Disabled Menu Items has a "D" prefix where Enabled Menu Items have "E" prefix
	virtual bool                 getObjectContextMenu(UTFStringVector &menuitems);
    /// This function is called when user selects a menuitem from ContextMenu
	virtual void                 onObjectContextMenu(int menuresult);
    /// Overridden by Editor Objects to Import Object Dependant Properties
    virtual void                 restore(Ogre::NameValuePairList &retList);
    /// Gets the Handle to encapsulated object
    inline virtual void         *getHandle() {return static_cast<void*>(m_Handle);};
    virtual Ogre::AxisAlignedBox getAABB();
    virtual void                 setSelectedImpl(bool bSelected);
    Ogre::Real                   hitTest(Ogre::Ray& ray, Ogre::Vector3& retPos);


protected:
	Ogre::Terrain   *m_Handle;
    Ogre::Vector3    m_Position;
    unsigned int     m_PageX;
    unsigned int     m_PageY;
    unsigned int     m_LayerCount;
    std::vector<TERRAINLAYERDATA> m_Layers;

	CPagedTerrainEditor();
	virtual     ~CPagedTerrainEditor();

    int         _getEmptyLayer();
    int         _createNewLayer(Ogre::String &texture, Ogre::String& normal);
    int         _getLayerID(Ogre::String& texture, Ogre::String& normal, bool dontcreate);
    void        _createDecalPass();

    void        _saveHeightmap();
    void        _saveBlendmaps();
};

class OgitorExport CPagedTerrainEditorFactory: public CBaseEditorFactory
{
public:
  CPagedTerrainEditorFactory();
  virtual ~CPagedTerrainEditorFactory() {};
  virtual void Init();
  virtual bool CanInstantiate();
  virtual CBaseEditor *CreateObject(CBaseEditor **parent, Ogre::NameValuePairList &params);
};
}

